package com.yanqu.road.server.manager.npc;

import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * npc
 * 仅内存测试用，不入库
 */
public class CrossNpcUserMgr {


    private static AtomicLong maxNpcId = new AtomicLong(100000000);

    private static RandomHelper randomHelper = new RandomHelper();

    /**
     * k:npcId
     */
    private static Map<Long, CrossUserBaseInfo> userBaseInfoMap = new ConcurrentHashMap<>();

    public static CrossUserBaseInfo getCrossNpcUserBaseInfo(long npcId) {
        return userBaseInfoMap.get(npcId);
    }

    public static boolean isNpc(long npcId) {
        return userBaseInfoMap.containsKey(npcId);
    }

    /**
     * 生成一个npc
     */
    public static CrossUserBaseInfo createCrossNpcUserBaseInfo() {
        return createCrossNpcUserBaseInfo(0);
    }

    /**
     * 生成一个npc
     * @param copyUserId 指定复制的用户,不需要传0就行
     */
    public static CrossUserBaseInfo createCrossNpcUserBaseInfo(long copyUserId) {
        CrossUserBaseInfo crossUserBaseInfo = new CrossUserBaseInfo();
        long npcId = maxNpcId.incrementAndGet();
        crossUserBaseInfo.setUserId(npcId);
        crossUserBaseInfo.setUserBaseInfo(initUserBaseInfo(npcId, copyUserId));
        crossUserBaseInfo.setLastUpdateTime(System.currentTimeMillis());
        userBaseInfoMap.put(npcId, crossUserBaseInfo);
        return crossUserBaseInfo;
    }

    public static UserBaseInfo initUserBaseInfo(long npcId, long copyUserId) {
        UserBaseInfo userBaseInfo = new UserBaseInfo();

        UserBaseInfo copyUserBaseInfo = null;
        if (copyUserId != 0) {
            copyUserBaseInfo = CrossUserMgr.getUserBaseInfo(copyUserId);
        }
        if (copyUserBaseInfo != null) {
            userBaseInfo.setUserAliasId(copyUserId);
            userBaseInfo.setNickName("npc" + npcId + "(" + copyUserBaseInfo.getNickName() + ")");
            userBaseInfo.setVipExp(copyUserBaseInfo.getVipExp());
            userBaseInfo.setTitleId(copyUserBaseInfo.getTitleId());
            userBaseInfo.setServerId(copyUserBaseInfo.getServerId());
            userBaseInfo.setDecoration(copyUserBaseInfo.getDecoration());
        } else {
            userBaseInfo.setNickName("npc" + npcId);
            userBaseInfo.setTitleId(randomHelper.next(30, 70));
            userBaseInfo.setVipExp(randomHelper.next(1, 999999));
            // 随机个服务器
            List<ServerInfo> serverInfoList = ServerListMgr.getOpenServerList(ConfigHelper.getInt("channelId"));
            userBaseInfo.setServerId(serverInfoList.get(randomHelper.next(0, serverInfoList.size())).getServerId());
            // 随机个头像
            userBaseInfo.setDecoration(randomGetNpcHeadId() + "@#$0@#$0@#$0@#$0");
        }
        return userBaseInfo;
    }


    private static int randomGetNpcHeadId() {
        List<Integer> list = new ArrayList<>();
        for (int i = 106055; i <= 106074; i++) {
            list.add(i);
        }
        return list.get(randomHelper.next(0, list.size()));
    }

}
